#include "inputbox.h"
#include <QDebug>
InputBox::InputBox(const QString & text,
                   const QPointF & pos,
                   QGraphicsItem * parent,
                   QGraphicsScene * scene,
                   const QFont & font,
                   const qreal textWidth) :
    QGraphicsTextItem(text, parent, scene)
{
    setDefaultTextColor(Qt::red);
    setPos(pos);
    setFont(font);
    setTextWidth(textWidth);
    setEnabled(true);
}

void InputBox::keyPressEvent(QKeyEvent *event)
{
//    qDebug() << event->key();
}

void InputBox::keyReleaseEvent(QKeyEvent *event)
{
    if (event->key()==Qt::Key_Backspace)
        deleteALetter();
    else if ( (event->key() >= 97 && event->key() <= 122) ||
              (event->key() >= 65 && event->key() <= 90) ||
              (event->key() >= 48 && event->key() <= 57) ||
             event->key() == Qt::Key_Minus ||
             event->key() == Qt::Key_Underscore)
        appendChar(event->key());
}

void InputBox::deleteALetter()
{
    QString temp = toPlainText();
    if (temp.length()==0)
        return;
    setPlainText(temp.remove(temp.size()-1, 1));
}

void InputBox::appendChar(int c)
{
    if (toPlainText().length()>10)
        return;
    setPlainText(toPlainText().append(QChar(c)));
}
